﻿@page "/dept"
@using daonaite.Model.system.dept
@using daonaite.ui.Api
@using daonaite.ui.Util
@inject IMessageService _message

@inject HttpHelp http
@inject RulesHelp rule
@inject apiDept depturl
<Card>
    <Form Model="@queryform" Layout="@FormLayout.Inline" OnFinish="queryfinish">
        <FormItem Label="部门名称">
            <Input @bind-Value="@context.deptName"></Input>
        </FormItem>
        <FormItem>
            <Button HtmlType="submit" Type="@ButtonType.Primary">搜索</Button>
        </FormItem>
    </Form>
</Card>
<Card>
    <Button OnClick="handladd">新增</Button>
    <Button Disabled="selects.Count() !=1" OnClick="async e=>{ await handlupdate(selects.First().id); }">编辑</Button>
    <Popconfirm Title="确认删除数据?"
                OnConfirm="async e=>{ await pldelete(); }">
        <Button Disabled="selects.Count() ==0">删除</Button>
    </Popconfirm>
    
    <Button>导入</Button>
    <Button>导出</Button>
</Card>
<Card>
    <Table DataSource="@treedept" TreeChildren="c=>c.child" PaginationPosition="null" @bind-SelectedRows="@selects">
        <Selection></Selection>
        <PropertyColumn Property="c=>c.id" Title="序号"></PropertyColumn>
        <PropertyColumn Property="c=>c.deptName" Title="部门名称"></PropertyColumn>
        <ActionColumn>
            <Button OnClick="async e=>{await handlupdate(context.id); }">编辑</Button>
            <Popconfirm Title="确认删除数据?"
                        OnConfirm="async e=>{ await delete(context.id); }">
                <Button>删除</Button>
            </Popconfirm>
            
        </ActionColumn>
    </Table>
</Card>
<Modal Title="@deptmodal.title" Visible="@deptmodal.open" OnCancel="deptmodal.cancel" Footer="null">
    <Form Model="@deptmodal.dept" ValidateMode="FormValidateMode.Rules" LabelColSpan="4" WrapperColSpan="20" OnFinish="formfinish">
        <FormItem Label="上级部门">
            <TreeSelect DataSource="@treedept" @bind-Value="@deptmodal.fid" ChildrenExpression="node=>node.DataItem.child"
                        TitleExpression="node=>node.DataItem.deptName"
                        KeyExpression="node=>node.DataItem.id.ToString()"></TreeSelect>
        </FormItem>
        <FormItem Label="部门名称" Rules="@rule.required("请输入部门名称")">
            <Input @bind-Value="@context.deptName" Placeholder="请输入部门名称"></Input>
        </FormItem>
        <FormItem WrapperColOffset="4">
            <Button Type="@ButtonType.Primary" HtmlType="submit">提交</Button>
            <Button OnClick="deptmodal.cancel">取消</Button>
        </FormItem>
    </Form>
</Modal>

@code {
    Dto_dept queryform = new Dto_dept();
    List<sys_dept> treedept = new List<sys_dept>();
    IEnumerable<sys_dept> selects = new List<sys_dept>();

    private class Modals
    {
        public string title { get; set; } = "";
        public bool open { get; set; } = false;
        public sys_dept dept { get; set; } = new sys_dept();
        public string fid { get; set; } = "";

        public void cancel()
        {
            open = false;

        }
    }

    Modals deptmodal = new Modals();


    protected override async Task OnInitializedAsync()
    {
        await queryfinish();
        await base.OnInitializedAsync();
    }


    private async Task queryfinish()
    {
        var result = await http.posttAsync<List<sys_dept>?, Dto_dept>(depturl.gettreedept, queryform);
        if (result.Code == 200)
        {
            treedept = result.Data;
        }
    }

    private async Task formfinish()
    {
        if (deptmodal.fid == "")
        {
            deptmodal.dept.fid = 0;
        }
        else
        {
            deptmodal.dept.fid = int.Parse(deptmodal.fid);
        }

        if (deptmodal.dept.id > 0) //编辑
        {
            var result = await http.posttAsync<int?, sys_dept>(depturl.update, deptmodal.dept);
            if (result.Code == 200)
            {
                _message.Success("修改成功");
                deptmodal.open = false;
                await queryfinish();
            }
        }
        else //新增
        {
            var result = await http.posttAsync<int?, sys_dept>(depturl.add, deptmodal.dept);
            if (result.Code == 200)
            {
                _message.Success("添加成功");
                deptmodal.open = false;
                await queryfinish();
            }
        }
    }

    private void handladd()
    {
        deptmodal.title = "添加部门";
        deptmodal.fid = "";
        deptmodal.open = true;
        deptmodal.dept = new sys_dept();
    }

    /// <summary>
    /// 更新部门按钮
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    private async Task handlupdate(int id)
    {
        var result = await http.getAsync<sys_dept?>($"{depturl.get}?id={id}");
        if (result.Code == 200)
        {
            deptmodal.title = "编辑部门";
            deptmodal.fid = result.Data.fid.ToString();
            deptmodal.open = true;
            deptmodal.dept = result.Data;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    private async Task delete(int id)
    {
        var result = await http.getAsync<int?>($"{depturl.delete}?id={id}");
        if (result.Code == 200)
        {
            _message.Success("删除成功");
            await queryfinish();
        }
    }

    private async Task pldelete()
    {
        foreach (var item in selects)
        {
            await http.getAsync<int?>($"{depturl.delete}?id={item.id}");
        }
        await queryfinish();
    }
}
